using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using LDBUtilities;

namespace ShoppingList
{
    public partial class AddItems : Form
    {
        private Dictionary<string, ShoppingItem> itemsToRemove = new Dictionary<string,ShoppingItem>();
        private Comparison<ListViewItem> lastSortMethod = Main.SortByChecked;
        private ShoppingList currentList;

        public AddItems()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Displays the dialog with all items. The items in the current list are pre-selected.
        /// If the ok button is pressed tha changes are made to the list datastructures.
        /// </summary>
        /// <param name="currentList"></param>
        /// <param name="allItems"></param>
        /// <returns></returns>
        public Dictionary<string, ShoppingItem> ShowDialog(ShoppingList currentList)
        {
            this.currentList = currentList;
            itemsToRemove.Clear();

            //display all items, check the pones that are in the list
            lsvItems.Items.Clear();
            foreach (ShoppingItem item in currentList.Items.Values)
            {
                ListViewItem lvItem = new ListViewItem(item.Name);
                lvItem.Tag = item;

                //check items already in list
                if (item.Inlist)
                {
                    lvItem.Checked = true;
                }
                lsvItems.Items.Add(lvItem);
            }
            Main.SortListView(lsvItems, lastSortMethod);

            //show dialog
            lblListName.Text = currentList.Name;
            ShowDialog();

            //make changes
            if (DialogResult == DialogResult.OK)
            {
                //set item inlist property based on status of checkbox
                foreach (ListViewItem lvItem in lsvItems.Items)
                {
                    ShoppingItem item = (ShoppingItem)lvItem.Tag;
                    item.Inlist = lvItem.Checked;
                }
            }
            else
            {
                itemsToRemove.Clear();      //if cancel, don't update or delete items
            }
            return itemsToRemove;
        }

        /// <summary>
        /// Adds a new item to the list of possible items
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNewItem_Click(object sender, EventArgs e)
        {
            if (txtNewItem.Text.Length > 0)
            {
                //create a new shopping item, that defaults to be in the list
                //TODO: check for duplicates
                ShoppingItem newItem = new ShoppingItem(txtNewItem.Text);
                newItem.Inlist = true;
                currentList.Items.Add(newItem.Name, newItem);

                ListViewItem lvItem = new ListViewItem(newItem.Name);
                lvItem.Checked = true;
                lvItem.Tag = newItem;

                lsvItems.Items.Add(lvItem);
                txtNewItem.Text = "";

                lsvItems.EnsureVisible(lsvItems.Items.IndexOf(lvItem));
            }
        }



        /// <summary>
        /// Called when the user clicks the cancel button.
        /// Closes the form without making any changes to the current list.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void miCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        /// <summary>
        /// Event handle for every time an input field gets the focus. Shows the SIP if the hardware keybpard is not enabled.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void input_GotFocus(object sender, EventArgs e)
        {
            if (0 < (Keyboard.GetKeyboardStatus() & Keyboard.KBDI_KEYBOARD_ENABLED))
            {
                inputPanel1.Enabled = true;
            }
        }

        /// <summary>
        /// Event handle for every time an input field loses the focus. Closes the SIP.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void input_LostFocus(object sender, EventArgs e)
        {
            inputPanel1.Enabled = false;
        }

        /// <summary>
        /// Called every time the SIP is opened or closed. Makes sure the input field is not hidden by the SIP
        /// by having an empty docked panel beneath it.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>        
        private void inputPanel1_EnabledChanged(object sender, EventArgs e)
        {
            SuspendLayout();
            pnlSipPlaceholder.Visible = inputPanel1.Enabled;
            pnlSipPlaceholder.Height = inputPanel1.Bounds.Height;
            ResumeLayout();
        }

        /// <summary>
        /// Called when the user clicks the delete item contect menu.
        /// Puts the item in the "to delete" list allowing the main form to delete the item from all lists
        /// when this dialog closes.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void miDelete_Click(object sender, EventArgs e)
        {
            DialogResult res = MessageBox.Show("Are you sure to delete item?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2);
            if (res == DialogResult.Yes)
            {
                foreach (int index in lsvItems.SelectedIndices)
                {
                    ShoppingItem itemToRemove = (ShoppingItem)lsvItems.Items[index].Tag;
                    itemsToRemove.Add(itemToRemove.Name, itemToRemove);
                    lsvItems.Items.RemoveAt(index);
                }
            }
        }

        /// <summary>
        /// Called when the popup contect menu opens. Enables/disables the correct menu items
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void contextMenu1_Popup(object sender, EventArgs e)
        {
            if (lsvItems.SelectedIndices.Count > 0) miDelete.Enabled = true;
            else miDelete.Enabled = false;
        }

        /// <summary>
        /// Checks for the return button, and if so calls newitem method.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtNewItem_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                btnNewItem_Click(null, null);
                e.Handled = true;
            }
        }

        /// <summary>
        /// Called when the user chooses to rename an item from the contect menu.
        /// Opens a dialog asking for the new name.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void miRename_Click(object sender, EventArgs e)
        {
            ShoppingItem itemToRename = (ShoppingItem)lsvItems.Items[lsvItems.SelectedIndices[0]].Tag;
            string newName = InputBox.Show("Enter new name", "Rename", itemToRename.Name);

            if (newName != string.Empty)
            {
                foreach (int index in lsvItems.SelectedIndices)
                {
                    ListViewItem lvi = lsvItems.Items[index];
                    itemToRename = (ShoppingItem)lvi.Tag;
                    lvi.Text = newName;
                    itemToRename.Name = newName;
                }
            }

        }

        private void lnkSortName_Click(object sender, EventArgs e)
        {
            Main.SortListView(lsvItems, Main.SortByName);
            lastSortMethod = Main.SortByName;
        }

        private void lnkSortChecked_Click(object sender, EventArgs e)
        {
            Main.SortListView(lsvItems, Main.SortByChecked);
            lastSortMethod = Main.SortByChecked;

        }

        private void lnkSortCheckout_Click(object sender, EventArgs e)
        {
            Main.SortListView(lsvItems, Main.SortByCheckout);
            lastSortMethod = Main.SortByCheckout;

        }

 


    }
}